function varargout = avg_params(varargin)
% $Id: avg_params.m 329 2005-05-09 20:31:24Z odellj $
% AVG_PARAMS Application M-file for avg_params.fig
%    FIG = AVG_PARAMS launch avg_params GUI.
%    AVG_PARAMS('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.5 13-Jun-2003 13:40:35
% $Log: avg_params.m,v $
% Revision 1.1.1.1  2004/02/18 20:09:49  odellj
% initial import into CVS
%
% Revision 1.2  2004/02/05 20:19:51  odellj
% Added Id and Log tags.
%

global hdr

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'reuse');

	% Use system color scheme for figure:
	set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
    
    set(handles.base_line_pts,'String',num2str(hdr.avg_baseline));
    set(handles.base_line_ms,'String',num2str(hdr.period_ms*hdr.avg_baseline));
    set(handles.Post_stim_pts,'String',num2str(hdr.epoch_length));
    set(handles.Post_stim_ms,'String',num2str(hdr.period_ms*hdr.epoch_length));
    set(handles.total_wind_pts,'String',num2str(hdr.avg_baseline+hdr.epoch_length));
    set(handles.total_wind_ms,'String',num2str(hdr.period_ms*(hdr.avg_baseline+hdr.epoch_length)));
    set(handles.n_conds,'String',num2str(hdr.n_conds));
    
	guidata(fig, handles);

	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		if (nargout)
			[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
		else
			feval(varargin{:}); % FEVAL switchyard
		end
	catch
		disp(lasterr);
	end

end

% --------------------------------------------------------------------
function varargout = base_line_ms_Callback(h, eventdata, handles, varargin)

global hdr
Value=get(h,'String');
%set(handles.base_line_ms,'String',Value);
hdr.avg_baseline=fix(str2num(Value)/hdr.period_ms);
set(handles.base_line_ms,'String',num2str(hdr.period_ms*hdr.avg_baseline));
set(handles.base_line_pts,'String',num2str(hdr.avg_baseline));
guidata(h,handles);
%
% Update total window fields
%
%fprintf('Updating total from base_line_ms\n')
tempa=str2num(get(handles.base_line_pts,'String'));
tempb=str2num(get(handles.Post_stim_pts,'String'));
total_wind_pts=tempa+tempb;

set(handles.total_wind_pts,'String',num2str(total_wind_pts));
tempc=str2num(get(handles.total_wind_pts,'String'));
total_wind_ms=hdr.period_ms*tempc;
set(handles.total_wind_ms,'String',num2str(total_wind_ms));

guidata(h,handles);

% --------------------------------------------------------------------
function varargout = base_line_pts_Callback(h, eventdata, handles, varargin)

global hdr
Value=get(h,'String');
set(handles.base_line_pts,'String',Value);
hdr.avg_baseline=str2num(Value);
set(handles.base_line_ms,'String',num2str(fix(hdr.avg_baseline*hdr.period_ms)));
guidata(h,handles);
%
% Update total window fields
%
tempa=str2num(get(handles.base_line_pts,'String'));
tempb=str2num(get(handles.Post_stim_pts,'String'));
total_wind_pts=tempa+tempb;

set(handles.total_wind_pts,'String',num2str(total_wind_pts));
tempc=str2num(get(handles.total_wind_pts,'String'));
total_wind_ms=hdr.period_ms*tempc;
set(handles.total_wind_ms,'String',num2str(total_wind_ms));

guidata(h,handles);

% --------------------------------------------------------------------
function varargout = Post_stim_ms_Callback(h, eventdata, handles, varargin)

global hdr
Value=get(h,'String');
post_stim_pts=fix(str2num(Value)/hdr.period_ms);
set(handles.Post_stim_pts,'String',num2str(post_stim_pts));
post_stim_ms=fix(post_stim_pts*hdr.period_ms);
set(handles.Post_stim_ms,'String',num2str(post_stim_ms));
guidata(h,handles);
%
% Update total window fields
%
tempa=str2num(get(handles.base_line_pts,'String'));
tempb=str2num(get(handles.Post_stim_pts,'String'));
total_wind_pts=tempa+tempb;

set(handles.total_wind_pts,'String',num2str(total_wind_pts));
tempc=str2num(get(handles.total_wind_pts,'String'));
total_wind_ms=hdr.period_ms*tempc;
set(handles.total_wind_ms,'String',num2str(total_wind_ms));

guidata(h,handles);

% --------------------------------------------------------------------
function varargout = Post_stim_pts_Callback(h, eventdata, handles, varargin)

global hdr
Value=get(h,'String');
set(handles.Post_stim_pts,'String',Value);
hdr.epoch_length=str2num(Value);
set(handles.Post_stim_ms,'String',num2str(fix(hdr.period_ms*hdr.epoch_length)));
guidata(h,handles);
%
% Update total window fields
%
tempa=str2num(get(handles.base_line_pts,'String'));
tempb=str2num(get(handles.Post_stim_pts,'String'));
total_wind_pts=tempa+tempb;

set(handles.total_wind_pts,'String',num2str(total_wind_pts));
tempc=str2num(get(handles.total_wind_pts,'String'));
total_wind_ms=hdr.period_ms*tempc;
set(handles.total_wind_ms,'String',num2str(total_wind_ms));
guidata(h,handles);

% --------------------------------------------------------------------
function varargout = total_wind_ms_Callback(h, eventdata, handles, varargin)

% If user changes total window, adjust Post_stim window, not baseline

global hdr
Value=get(h,'String');

total_wind_pts=fix(str2num(Value)/hdr.period_ms);
set(handles.total_wind_pts,'String',num2str(total_wind_pts));
total_wind_ms=hdr.period_ms*total_wind_pts;
set(handles.total_wind_ms,'String',num2str(total_wind_ms));
guidata(h,handles);
%
% Update Post_stim fields
%
tempa=str2num(get(handles.total_wind_pts,'String'));
tempb=str2num(get(handles.base_line_pts,'String'));
post_stim_pts=tempa-tempb;
set(handles.Post_stim_pts,'String',num2str(post_stim_pts));
post_stim_ms=hdr.period_ms*post_stim_pts;
set(handles.Post_stim_ms,'String',num2str(post_stim_ms));
guidata(h,handles);

% --------------------------------------------------------------------
function varargout = total_wind_pts_Callback(h, eventdata, handles, varargin)

% If user changes total window, adjust Post_stim window, not baseline

global hdr
Value=get(h,'String');
set(handles.total_wind_pts,'String',Value);
total_wind=str2num(Value);
set(handles.total_wind_ms,'String',num2str(fix(hdr.period_ms*total_wind)));
guidata(h,handles);
%
% Update Post_stim fields
%
tempa=str2num(get(handles.total_wind_pts,'String'));
tempb=str2num(get(handles.base_line_pts,'String'));
post_stim_pts=tempa-tempb;
set(handles.Post_stim_pts,'String',num2str(post_stim_pts));
post_stim_ms=hdr.period_ms*post_stim_pts;
set(handles.Post_stim_ms,'String',num2str(post_stim_ms));
guidata(h,handles);

% --------------------------------------------------------------------
function varargout = done_Callback(h, eventdata, handles, varargin)
global hdr

% update hdr variables
%
hdr.avg_baseline=str2num(get(handles.base_line_pts,'String'));
hdr.epoch_length=str2num(get(handles.Post_stim_pts,'String'));

close


% --- Executes during object creation, after setting all properties.
function n_conds_CreateFcn(hObject, eventdata, handles)

if ispc
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));% or 'White'
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% define hdr.n_conds
%
function n_conds_Callback(hObject, eventdata, handles)
global hdr

Value=get(hObject,'String');
set(handles.n_conds,'String',Value);
hdr.n_conds=str2num(Value);
guidata(h,handles);


